Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 所有面试分类
    allCategories: [
      { 
        id: 1, 
        name: "前端开发", 
        icon: "/images/frontend.svg", 
        color: "#4080FF", 
        isRecommended: true,
        children: [
          { id: 101, name: "Vue开发", difficulty: 3, icon: "/images/vue.svg", color: "#41B883" },
          { id: 102, name: "React开发", difficulty: 3, icon: "/images/react.svg", color: "#61DAFB" },
          { id: 103, name: "Angular开发", difficulty: 4, icon: "/images/angular.svg", color: "#DD0031" },
          { id: 104, name: "HTML/CSS/JS基础", difficulty: 2, icon: "/images/htmlcssjs.svg", color: "#F0DB4F" }
        ]
      },
      { 
        id: 2, 
        name: "后端开发", 
        icon: "/images/backend.svg", 
        color: "#722ED1", 
        isRecommended: true,
        children: [
          { id: 201, name: "Java开发", difficulty: 4, icon: "/images/java.svg", color: "#5382A1" },
          { id: 202, name: "Python开发", difficulty: 3, icon: "/images/python.svg", color: "#306998" },
          { id: 203, name: "Node.js开发", difficulty: 3, icon: "/images/nodejs.svg", color: "#68A063" },
          { id: 204, name: "Go开发", difficulty: 4, icon: "/images/go.svg", color: "#00ADD8" }
        ]
      },
      { 
        id: 3, 
        name: "移动开发", 
        icon: "/images/mobile.svg", 
        color: "#0FC6C2", 
        isRecommended: true,
        children: [
          { id: 301, name: "iOS开发", difficulty: 4, icon: "/images/ios.svg", color: "#007AFF" },
          { id: 302, name: "Android开发", difficulty: 4, icon: "/images/android.svg", color: "#3DDC84" },
          { id: 303, name: "跨平台开发", difficulty: 3, icon: "/images/crossplatform.svg", color: "#6554C0" }
        ]
      },
      { 
        id: 4, 
        name: "测试开发", 
        icon: "/images/testing.svg", 
        color: "#FF7D00", 
        isRecommended: true,
        children: [
          { id: 401, name: "功能测试", difficulty: 2, icon: "/images/functionaltest.svg", color: "#8FC5E9" },
          { id: 402, name: "自动化测试", difficulty: 4, icon: "/images/autotest.svg", color: "#5CACEE" },
          { id: 403, name: "性能测试", difficulty: 4, icon: "/images/perftest.svg", color: "#00B42A" }
        ]
      },
      { id: 5, name: "数据结构", icon: "/images/datastructure.svg", color: "#F53F3F", isRecommended: false, children: [] },
      { id: 6, name: "算法设计", icon: "/images/algorithm.svg", color: "#7CB305", isRecommended: false, children: [] },
      { id: 7, name: "数据库", icon: "/images/database.svg", color: "#C1232B", isRecommended: false, children: [] },
      { id: 8, name: "计算机网络", icon: "/images/network.svg", color: "#27727B", isRecommended: false, children: [] },
      { id: 9, name: "操作系统", icon: "/images/os.svg", color: "#FAA755", isRecommended: false, children: [] },
      { id: 10, name: "机器学习", icon: "/images/ml.svg", color: "#60C0DD", isRecommended: false, children: [] },
      { id: 11, name: "产品经理", icon: "/images/product.svg", color: "#8A959E", isRecommended: false, children: [] },
      { id: 12, name: "UI/UX设计", icon: "/images/design.svg", color: "#B37FEB", isRecommended: false, children: [] },
    ],
    
    // 推荐分类
    recommendedCategories: [],
    
    // 过滤后的分类
    filteredCategories: [],
    
    // 搜索关键词
    searchKeyword: "",
    
    // 当前激活的分类ID
  activeCategoryId: null,
  
  // 当前分类层级：1-一级分类，2-二级分类
  currentLevel: 1,
  
  // 选中的一级分类ID
  selectedParentId: null,
  
  // 选中的一级分类名称
  selectedParentName: ""
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 初始化分类数据
    this.initCategories();
  },
  
  /**
   * 初始化分类数据
   */
  initCategories: function() {
    // 筛选推荐分类（一级分类）
    const recommendedCategories = this.data.allCategories.filter(category => category.isRecommended);
    
    this.setData({
      recommendedCategories: recommendedCategories,
      filteredCategories: this.data.allCategories // 初始显示所有一级分类
    });
  },
  
  /**
   * 搜索输入事件处理
   */
  onSearchInput: function(e) {
    const keyword = e.detail.value.trim().toLowerCase();
    
    this.setData({
      searchKeyword: keyword
    });
    
    if (keyword) {
      if (this.data.currentLevel === 1) {
        // 一级分类搜索
        const filtered = this.data.allCategories.filter(category => 
          category.name.toLowerCase().includes(keyword)
        );
        this.setData({ filteredCategories: filtered });
      } else {
        // 二级分类搜索
        const parentCategory = this.data.allCategories.find(cat => cat.id === this.data.selectedParentId);
        if (parentCategory) {
          const filteredChildren = parentCategory.children.filter(child => 
            child.name.toLowerCase().includes(keyword)
          );
          this.setData({ filteredCategories: filteredChildren });
        }
      }
    } else {
      if (this.data.currentLevel === 1) {
        this.setData({ filteredCategories: this.data.allCategories });
      } else {
        const parentCategory = this.data.allCategories.find(cat => cat.id === this.data.selectedParentId);
        this.setData({ filteredCategories: parentCategory ? parentCategory.children : [] });
      }
    }
  },
  
  /**
   * 重置搜索
   */
  resetSearch: function() {
    this.setData({
      searchKeyword: "",
      filteredCategories: this.data.allCategories
    });
  },
  
  /**
   * 开始面试
   */
  startInterview: function(e) {
    const category = {
      id: e.currentTarget.dataset.id,
      name: e.currentTarget.dataset.name,
      difficulty: e.currentTarget.dataset.difficulty,
      icon: e.currentTarget.dataset.icon
    };
    
    // 保存当前选中的分类
    wx.setStorageSync('currentCategory', category);
    
    // 跳转到面试页面
    wx.navigateTo({
      url: '/pages/interview/interview'
    });
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    
  },
  
  /**
   * 用户点击右上角分享
   */
  /**
   * 选择一级分类，进入二级分类视图
   */
  selectParentCategory: function(e) {
    const parentId = e.currentTarget.dataset.id;
    const parentCategory = this.data.allCategories.find(cat => cat.id === parentId);
    
    this.setData({
      currentLevel: 2,
      selectedParentId: parentId,
      selectedParentName: parentCategory.name,
      filteredCategories: parentCategory.children || []
    });
  },
  
  /**
   * 返回一级分类视图
   */
  goBackToLevel1: function() {
    this.setData({
      currentLevel: 1,
      selectedParentId: null,
      selectedParentName: '',
      filteredCategories: this.data.allCategories,
      searchKeyword: ''
    });
  },
  
  onShareAppMessage: function() {
    
  }
})
    